package com.so206.controller.payment;

import com.github.pagehelper.PageInfo;
import com.so206.plugins.pay.AliPayFunction;
import com.so206.po.AlipayConfig;
import com.so206.po.AlipayTradeConfig;
import com.so206.po.AlipayTradeRecord;
import com.so206.service.AlipayConfigService;
import com.so206.service.AlipayTradeServices;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝 --资金归集相关
 */
@Controller
@RequestMapping("/admin/trade")
public class AlipayTradeController {

    @Autowired
    private AlipayTradeServices alipayTradeServices;

    @Autowired
    private AliPayFunction aliPayFunction;

    @Autowired
    private AlipayConfigService alipayConfigService;

    /**
     * 归集页面
     */
    @RequiresPermissions("alipay_trade")
    @RequestMapping("/edit")
    public String info(Model model, Integer status, String tr_name, String tr_account, String sid,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer rows,
                       @RequestParam(defaultValue = "false") Boolean ajax) {
        AlipayTradeConfig getconfig = alipayTradeServices.getconfig(1L);
        PageInfo<AlipayTradeRecord> PageInfo = alipayTradeServices.find_record_by_page(page, rows, status, tr_name, tr_account, sid);
        model.addAttribute("bean", PageInfo);
        model.addAttribute("config", getconfig);
        model.addAttribute("status", status);
        model.addAttribute("tr_name", tr_name);
        model.addAttribute("tr_account", tr_account);
        model.addAttribute("sid", sid);
        if (ajax) {
            return "admin/trade/edit::pageNode";
        } else {
            return "admin/trade/edit";
        }
    }

    /**
     * 更新配置
     */
    @RequiresPermissions("alipay_trade")
    @RequestMapping("/update_config")
    @ResponseBody
    public Map update_config(AlipayTradeConfig config) {
        HashMap<Object, Object> ret = new HashMap<>();
        alipayTradeServices.update_config(config);
        ret.put("msg", "修改成功!");
        ret.put("code", 0);
        return ret;
    }

    /**
     * 删除记录
     */
    @RequiresPermissions("alipay_trade")
    @RequestMapping("/delete")
    @ResponseBody
    public Map ddelete_record(Long id) {
        HashMap<Object, Object> ret = new HashMap<>();
        alipayTradeServices.delete_record_by_id(id);
        ret.put("msg", "删除成功!");
        ret.put("code", 0);
        return ret;
    }

    /**
     * 查看记录
     */
    @RequiresPermissions("alipay_trade")
    @RequestMapping("/view")
    public String view_record(Long id, Model model) {
        AlipayTradeRecord record = alipayTradeServices.find_record_by_id(id);
        model.addAttribute("model", record);
        AlipayConfig config = alipayConfigService.find_by_id(record.getConnId());
        model.addAttribute("config", config);
        return "admin/trade/view";
    }


    /**
     * 重新打款
     */
    @RequiresPermissions("alipay_trade")
    @RequestMapping("/re_tr")
    @ResponseBody
    public Map re_tr(Long id) {
        HashMap<Object, Object> ret = new HashMap<>();
        AlipayTradeRecord model = alipayTradeServices.find_record_by_id(id);
        if (model.getStatus() == 1) {
            ret.put("msg", "已打款成功!");
            ret.put("code", 0);
            return ret;
        } else {
            HashMap<String, String> param = new HashMap<>();
            param.put("final_money", String.valueOf(model.getTrMoney().subtract(model.getSettChange())));
            param.put("sid", model.getOutOrderId());
            param.put("pid", model.getTrAccount());
            param.put("pname", model.getTrName());
            param.put("ip", "127.0.0.1");
            param.put("title", model.getTrDesc());
            AlipayConfig alipayConfig = alipayConfigService.find_by_id(model.getConnId());
            Map money = aliPayFunction.changeMoneyToAccountMap(param, alipayConfig.getAppId(), alipayConfig.getPrivateKey(), alipayConfig.getPublicKey());
            if (money == null) {
                ret.put("msg", "请求支付宝失败,原因未知!");
                ret.put("code", -1);
                return ret;
            }
            Object error = money.get("error");
            Integer status = (Integer) money.get("status");
            Object tran_no = money.get("tran_no");
            if (status == null) {
                model.setStatus(0);
                model.setTradeStatus("ERROR");
                model.setErrInfo(String.valueOf(error));
                alipayTradeServices.update_record(model);
                ret.put("msg", "请求支付宝失败,原因:" + error);
                ret.put("code", -1);
                return ret;
            }
            if (status == 2) {
                model.setStatus(1);
                model.setTradeStatus("SUCCESS");
                model.setErrInfo("SUCCESS");
                model.setTradeNo(String.valueOf(tran_no));
                ret.put("msg", "打款成功!");
                ret.put("code", 0);
            } else {
                model.setStatus(0);
                model.setTradeStatus("ERROR");
                model.setErrInfo(String.valueOf(error));
                ret.put("msg", "请求支付宝失败,原因:" + error);
                ret.put("code", 0);
            }
            alipayTradeServices.update_record(model);

        }
        return ret;
    }

}
